<?php

class Registration_IndexController
        extends Zend_Controller_Action {

    /**
     * Обслуживание попыток регистрации
     */
    public function indexAction() {
        $form = new Registration_Form_Registration();
        if (!$this->_request->isPost()) {
            $this->view->form = $form;
        }
        else {
            if (!$form->isValid($_POST)) {
                $this->view->form = $form;
                $this->view->message =
                        Registration_Service::FAIL_DATA;
            }
            else {
                $candidate = $form->getValues();
                $registration_model =
                        new Registration_Service();
                if (!$registration_model->existsUser($candidate)) {
                    if (!$registration_model->existsCandidate($candidate)) {
                        if ($registration_model->insertCandidate($candidate)) {
                            $this->view->message =
                                    Registration_Service::REGISTRATION_COMPLETE;
                        }
                        else {
                            $this->view->message =
                                    Registration_Service::DB_ERROR;
                        }
                    }
                    else {
                        $this->view->message =
                                Registration_Service::CANDIDATE_EXISTS;
                    }
                }
                else {
                    $this->view->message =
                            Registration_Service::USER_EXISTS;
                }
            }
        }
    }

}